using System;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimplePanelCallback {
	//[ToolboxItem(true)]
	///<summary>
	/// Panel callback control
	///</summary>
	[ToolboxBitmap(typeof(SpPanelCallback), "Library.WebControls.SimplePanelCallback.SpPanelCallback.bmp")]
	[ControlBuilder(typeof(PlaceHolderControlBuilder))]
	[Designer(typeof(SpPanelCallbackDesgin))]
	[ParseChildren(true, "Content")]
	[DesignTimeVisible(true)]
	public class SpPanelCallback : BaseAjaxControl {
		protected const string SpPanelCallbackCallbackParameter = "__SPPANELCALLBACKRQP_";
		protected const string SpPanelCallbackCallbackData = "__SPPANELCALLBACKD_";
		protected const string SpPanelCallbackCallbackFucntion = "__SPPANELCALLBACKF_";
		protected const string SpPanelCallbackCallbackServerId = "__SPPANELCALLBACKS_";
		internal static Type SpPanelCallbackControlType = typeof(SpPanelCallback);
		/// <summary>
		/// Check is Panel callback
		/// </summary>
		/// <param name="page"></param>
		/// <returns></returns>
		public static bool IsSpPanelCallbackCallback(Page page) {
			return page.Request[SpPanelCallbackCallbackParameter] == "T";
		}
		/// <summary>
		/// Get panel callback
		/// </summary>
		public override bool IsCallback {
			get {
				return _isCallback;
			}
		}
		private HtmlGenericControl _content;



		protected override void CreateChildControls() {
			_content = new HtmlGenericControl("div");
			Controls.Add(_content);

		}
		/// <summary>
		/// Panel content
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		public HtmlGenericControl Content {
			get {
				EnsureChildControls();
				return _content;
			}
		}
		/// <summary>
		/// Css class
		/// </summary>
		public string CssClass {
			get {
				EnsureChildControls();
				return _content.Attributes["class"];
			}
			set {
				EnsureChildControls();
				_content.Attributes["class"] = value;
			}
		}

		public override string ID {
			get {
				EnsureChildControls();
				return _content.ID;
			}
			set {
				EnsureChildControls();
				_content.ID = value;
			}
		}

		public override string ClientID {
			get {
				EnsureChildControls();
				return _content.ClientID;
			}
		}


		protected string PanelClientCallbackFunction;
		private bool _isCallback;

		protected string CallbackParameter;
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);
			if (Request != null) {
				_isCallback = IsSpPanelCallbackCallback(Page) && ClientID == Request[SpPanelCallbackCallbackServerId];
				if (_isCallback) {
					PanelClientCallbackFunction = Page.Request[SpPanelCallbackCallbackFucntion];
				}
			} else {
				_isCallback = false;
			}

		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			if (IsCallback) {
				switch (PanelClientCallbackFunction) {
					case "C"://Custom callback
						OnCustomCallback(new CustomCallbackEventArgs(Request[SpPanelCallbackCallbackData]));
						break;
				}
			}

			IncludeScriptOnLoad(SpPanelCallbackControlType, "PN", "Library.WebControls.SimplePanelCallback.include.SpPanelCallback.js", UtilLib);
		}


		protected virtual void OnCustomCallback(CustomCallbackEventArgs e) {
			CustomCallbackEventHandler handler = CustomCallback;
			if (handler != null) {
				try {
					handler(this, e);
					ProcessCustomCallbackEventArgs(e);
				} catch (Exception ex) {
					WriteError(ex);
				}
			}
		}




		protected override void CreateClientObjectAtRender(HtmlTextWriter writer) {
			BeginScriptTag(writer);
			writer.Write("{0}=new SpPanelCallback();", ClientName);
			writer.Write("{0}.Init('{0}', '{1}');", ClientName, ClientID);
			EndScriptTag(writer);
		}
		protected override void Render(HtmlTextWriter writer) {
			if (!Visible)
				return;
			if (IsCallback) {
				WriteCustomCallbackCompleteResult();
				foreach (Control ctr in _content.Controls) {
					ctr.RenderControl(writer);
				}

				FlushAndEndCallback();
			} else {
				//writer.Write("<div id='" + ClientID + "'>");
				base.Render(writer);
				//writer.Write("</div>");
			}
		}
		/// <summary>
		/// 
		/// </summary>
		public event CustomCallbackEventHandler CustomCallback;
	}
}
